package com.lhz.framework.exception;


import com.lhz.common.constant.ResponseState;
import com.lhz.common.page.ResponseObject;
import lombok.extern.slf4j.Slf4j;
import cn.hutool.core.util.StrUtil;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.List;


/**
 * 全局异常处理
 * Created by LiGezZ
 * Date on 2021/4/17 11:07
 */
@RestControllerAdvice
@Slf4j
public class GlobalException {
    /**
     * 服务异常
     */
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(Exception.class)
    public ResponseObject handlerException(Exception e) {
        log.error(e.getMessage());
        return ResponseObject.fail(ResponseState.EXCEPTION);
    }

    //授权异常
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(TokenException.class)
    public ResponseObject handleTokenException(TokenException e) {
        log.error(e.getMessage());
        return ResponseObject.fail(e.getMessage(), e.getCode());
    }

    //自定义异常
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(ServiceException.class)
    public ResponseObject handleException(ServiceException e) {
        log.error(e.getMessage());
        return ResponseObject.fail(e.getMessage(), e.getCode());
    }

    /**
     * 服务异常
     */
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(IllegalArgumentException.class)
    public ResponseObject handlerException(IllegalArgumentException e) {
        ResponseState responseSate = ResponseState.getResponseSate(e.getMessage());
        if (responseSate != null) {
            log.error(responseSate.getName());
            return ResponseObject.fail(responseSate);
        }
        log.error(e.getMessage());
        return ResponseObject.fail(e.getMessage());
    }

    /**
     * 参数校验异常
     */
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(MethodArgumentNotValidException.class)
    //（Bean 校验异常）
    public ResponseObject handlerMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        log.error(e.getMessage());
        BindingResult bindingResult = e.getBindingResult();

        // 所有参数异常信息
        List<ObjectError> allErrors = bindingResult.getAllErrors();
        if (!allErrors.isEmpty()) {
            return ResponseObject.fail(ResponseState.EXCEPTION.getCode(), allErrors.get(0).getDefaultMessage());
        }


        return ResponseObject.fail(ResponseState.EXCEPTION);
    }

    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(BindException.class)
    //参数绑定异常
    public ResponseObject handlerMethodArgumentNotValidException(BindException e) {
        log.error(e.getMessage());
        BindingResult bindingResult = e.getBindingResult();

        // 所有参数异常信息
        List<ObjectError> allErrors = bindingResult.getAllErrors();
        if (!allErrors.isEmpty()) {
            return ResponseObject.fail(ResponseState.EXCEPTION.getCode(), allErrors.get(0).getDefaultMessage());
        }


        return ResponseObject.fail(ResponseState.EXCEPTION);
    }

    /**
     * 数据访问异常 DataAccessException
     */
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(DataAccessException.class)
    public ResponseObject handlerDataAccessException(DataAccessException e) {
        log.error(e.getMessage());
        if (e instanceof DuplicateKeyException) {
            return ResponseObject.fail(ResponseState.EXCEPTION.getCode(), "主键唯一性校验失败");
        }

        if (e instanceof DataIntegrityViolationException) {
            String message = e.getMessage();
            if (StrUtil.contains(message, "Data too long for column")) {
                return ResponseObject.fail(ResponseState.EXCEPTION.getCode(), "输入字符数超过指定长度");
            }
        }


        return ResponseObject.fail(ResponseState.EXCEPTION);
    }

}
